package com.jt.service;

import com.jt.mapper.TeacherMapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.Teacher;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TeacherServicempl implements TeacherService{
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public List<Teacher> findAll() {
        return teacherMapper.findAll();
    }

    /**
     * 要求查询  1页10条
     * 特点: 数组的结果  口诀: 含头不含尾
     * 语  法:  select * from user limit 起始位置,查询的条数
     * 第一页:  select * from user limit 0,10       0-9
     * 第二页:  select * from user limit 10,10      10-19
     * 第三页:  select * from user limit 20,10      20-29
     * 第N页:   select * from user limit (n-1)*10,10
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getUserList(PageResult pageResult) {
        //1.记录总数 total
        long total = teacherMapper.getTotal();
        //2.分页后的数据
        //2.1获取每页条数
        int size = pageResult.getPageSize();
        //2.2获取起始位置
        int start = (pageResult.getPageNum() - 1) * size;
        //2.3 获取用户查询的数据
        String query = pageResult.getQuery();
        List<Teacher> rows = teacherMapper.findUserListByPage(start,size,query);
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    public Teacher findUserById(Integer id) {

        return teacherMapper.findUserById(id);
    }

    //id/phone/email
    @Override
    @Transactional
    public void updateUser(Teacher teacher) {
        /*teacher.setUpdated(new Date());*/
        teacherMapper.updateUser(teacher);
    }

    //面试题: 常见运行时异常   常见检查异常(编译异常)
    //@Transactional(rollbackFor = IOException.class)      //事务的注解
    @Transactional
    @Override
    public void deleteUserById(Integer id){

        teacherMapper.deleteUserById(id);
        //int a = 1/0;
    }

}
